package sopt.dev.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import sopt.dev.view.CityItem;
import sopt.dev.view.CityItem.OnCityClickListener;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class CityAdatper extends BaseAdapter implements OnCityClickListener , IAdapterController{

	Context mContext;
	List<CityVO<ItemVO>> mData = new ArrayList<CityVO<ItemVO>>();
	
	public interface OnCityAdapterListener {
		void onItemClick(CityVO<ItemVO> data);
	}
	
	OnCityAdapterListener mListener;
	
	public void setOnCityAdapterListener(OnCityAdapterListener listener) {
		this.mListener = listener;
	}
	
	public CityAdatper(Context context) {
		this.mContext = context;
	}
	
	public CityAdatper(Context context, List<CityVO<ItemVO>> items) {
		this.mContext = context;
		for(CityVO<ItemVO> item : items) {
			mData.add(item);
		}
		notifyDataSetChanged();
	}
	
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		CityItem itemView = (CityItem)convertView;
		
		if(itemView == null) {
			itemView = new CityItem(mContext);
			itemView.setOnCityClickListener(this);
		}
		itemView.setData((CityVO)getItem(position));
		
		return itemView;
	}

	@Override
	public void onCityClick(CityVO<ItemVO> city) {
		if(mListener != null) {
			mListener.onItemClick(city);
		}		
	}


	@Override
	public <T> void remove(T deletedItem) {
		mData.remove(deletedItem);
	}

	@Override
	public <T> void add(T item, int position) {
		mData.add(position, (CityVO<ItemVO>) item);
		notifyDataSetChanged();
	}

	@Override
	public <T> void add(T item) {
		mData.add((CityVO<ItemVO>) item);
		notifyDataSetChanged();		
	}

	@Override
	public <T> void add(List<T> items) {
		mData.addAll((Collection<? extends CityVO<ItemVO>>) items);
		notifyDataSetChanged();
	}

	@Override
	public <T> T getItemByIndex(int position) {
		return (T) mData.get(position);
	}


}
